﻿'use strict';
/**
 * controllers for dynamic table
 * Remove/delete a table row dynamically 
 */

app.controller("ListAdvertisementController", ListAdvertisementController);
ListAdvertisementController.$inject = ['$scope', '$state', 'ngTableParams', 'advertisementService'];
function ListAdvertisementController($scope, $state, ngTableParams, advertisementService) {
    var vm = this;
    vm.tableParams = new ngTableParams({
        page: 1,
        count: 10
    }, {
        getData: function ($defer, params) {
            advertisementService.get({ pageSize: params.count(), pageIndex: params.page() }, function (resp) {
                vm.tableParams.total(resp.data.total);
                $defer.resolve(resp.data.list);
            });
        }
    });

    vm.removeRow = function (advertisement) {
        var advertisements = vm.tableParams.data;
        var index = advertisements.indexOf(advertisement);
        advertisementService.delete({ advertisementId: advertisement.id }, function () {
            advertisements.splice(index, 1);
        });
    };
};